package com.abu.messageboard.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.PageUtil;
import com.abu.common.Resp;
import com.abu.messageboard.config.mvc.LoginInterceptor;
import com.abu.messageboard.pojo.Message;
import com.abu.messageboard.service.MessageService;
import com.abu.messageboard.utils.PageUtils;
import com.abu.messageboard.vo.QueryMessageBoardVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 留言表 前端控制器
 * </p>
 *
 * @author 阿布
 * @since 2021-02-25
 */
@RestController
@RequestMapping("/board")
public class MessageBoardController {

    @Autowired
    private MessageService messageService;

    @GetMapping("/page/condition")
    public Resp queryByPageAndCondition(QueryMessageBoardVo condition) {
        HashMap<String, Object> mapResult = this.messageService.queryByPageAndCondition(condition);
        return Resp.ok().data("mapResult", mapResult);
    }

    @GetMapping("/noAuthorize/messageCount/{userId}")
    public Resp queryMessageCount(@PathVariable("userId") Long userId) {
        int count = this.messageService.count(new QueryWrapper<Message>().eq("user_id", userId));
        return Resp.ok().data("count", count);
    }

    @GetMapping("all/{applyId}")
    public Resp queryAllMessageByIds(@PathVariable("applyId") Long applyId) {
        String managerId = LoginInterceptor.getManagerId();
        List<Message> messageList = this.messageService.list(new QueryWrapper<Message>()
                .eq("manager_id", managerId)
                .eq("apply_report_id", applyId)
                .select("gmt_create", "content"));
        return Resp.ok().data("messageList", messageList);
    }

    @PostMapping("entity")
    public Resp addMessage(
            @RequestParam("applyId") Long applyId,
            @RequestParam("content") String content,
            @RequestParam("userId") Long userId
    ) {
        String managerId = LoginInterceptor.getManagerId();
        Message message = new Message();
        message
                .setManagerId(Convert.toLong(managerId))
                .setApplyReportId(applyId)
                .setContent(content)
                .setUserId(userId);
        boolean flag = this.messageService.save(message);
        if (flag) {
            return Resp.ok().msg("留言成功");
        }
        return Resp.error().msg("留言失败");
    }

}

